﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using System.Text;
using System.IO;
using System.Xml;
using BLL;
using DAL;
using Models;
using System.Data;

public partial class cn_member_center_user_address : System.Web.UI.Page
{
    // Fields

    protected RegionManager rm = new RegionManager();
    protected UserAddressManager uam = new UserAddressManager();

    // Methods
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session["user"] == null)
        {
            base.Response.Redirect("/cn/web_login.html?ru=" + HttpContext.Current.Request.Url.PathAndQuery.Replace("aspx", "html"));
        }
        if (!base.IsPostBack)
        {
            this.rptUserAddressBind();
        }
    }

    private void rptUserAddressBind()
    {
        if (this.Session["user"] != null)
        {
            DataTable userAddressByUser = this.uam.GetUserAddressByUser((this.Session["user"] as User).Id);
            this.rptUserAddress.DataSource = userAddressByUser.DefaultView;
            this.rptUserAddress.DataBind();
        }
    }

    protected StringBuilder UserAddressBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable regionByParentRegionAndLanguageVersion;
            if (base.Request.QueryString["id"] != null)
            {
                regionByParentRegionAndLanguageVersion = this.uam.GetUserAddressById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (regionByParentRegionAndLanguageVersion.Rows.Count > 0)
                {
                    DataRow row = regionByParentRegionAndLanguageVersion.Rows[0];
                    builder.AppendLine("<h5><strong>编辑收货地址</strong></h5>");
                    builder.AppendLine("<p><label for=\"txtConsignee\">收货人姓名：</label><input type=\"text\" id=\"txtConsignee\" maxlength=\"20\" value=\"" + row["consignee"] + "\" /><span id=\"spConsignee\">*</span></p>");
                    builder.AppendLine("<p><label for=\"selectProvinces\">省份/直辖市：</label>");
                    builder.AppendLine("<select id=\"selectProvinces\">");
                    builder.AppendLine("<option value=\"0\">请选择...</option>");
                    DataTable table2 = this.rm.GetRegionByParentRegionAndLanguageVersion(0, "zh-CN");
                    ArrayList list = ArrayList.Adapter(row["region"].ToString().Split(new char[] { ',' }));
                    foreach (DataRow row2 in table2.Rows)
                    {
                        builder.AppendLine(string.Concat(new object[] { "<option value=\"", row2["id"], "\" ", (row2["regionName"].ToString() == list[0].ToString()) ? "selected='selected'" : "", ">", row2["regionName"], "</option>" }));
                    }
                    builder.AppendLine("</select>");
                    builder.AppendLine("<span id=\"spanCity\" style=\"color: #000000;\">");
                    DataTable table3 = this.rm.GetRegionByParentRegionAndLanguageVersion(this.rm.GetRegionByParentRegionAndRegionName(0, list[0].ToString()), "zh-CN");
                    if (table3.Rows.Count > 0)
                    {
                        builder.AppendLine("市(县/区)：<select id=\"selectCity\">");
                        builder.AppendLine("<option value=\"0\">请选择</option>");
                        foreach (DataRow row3 in table3.Rows)
                        {
                            builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\" ", (row3["regionName"].ToString() == list[1].ToString()) ? "selected='selected'" : "", ">", row3["regionName"], "</option>" }));
                        }
                        builder.AppendLine("</select>");
                    }
                    builder.AppendLine("</span><span id=\"spProvinces\">*</span></p>");
                    builder.AppendLine("<p><label for=\"txtAddress\">详细地址：</label><input type=\"text\" id=\"txtAddress\" maxlength=\"200\" value=\"" + row["address"] + "\" /><span id=\"spAddress\">*</span></p>");
                    builder.AppendLine("<p><label for=\"txtZipcode\">邮政编码：</label><input type=\"text\" id=\"txtZipcode\" maxlength=\"20\" value=\"" + row["zipcode"] + "\" /><span id=\"spZipcode\">*</span></p>");
                    builder.AppendLine("<p><label for=\"txtTelephone\"> 联系电话：</label><input type=\"text\" id=\"txtTelephone\" maxlength=\"50\" value=\"" + row["telephone"] + "\" /><span id=\"spTelephone\"></span></p>");
                    builder.AppendLine("<p><label for=\"txtMobile\">手机号码：</label><input type=\"text\" id=\"txtMobile\" maxlength=\"50\" value=\"" + row["mobile"] + "\" /><span id=\"spMobile\">* 联系电话和手机号码至少填写一项</span></p>");
                    builder.AppendLine("<p class=\"radio_input\"><label>是否默认：</label><input id=\"radioIsDefault1\" type=\"radio\" name=\"isDefault\" " + ((Convert.ToInt32(row["isDefault"]) == 1) ? "checked='checked'" : "") + " value=\"1\" />是&nbsp;<input id=\"radioIsDefault0\" type=\"radio\" name=\"isDefault\" " + ((Convert.ToInt32(row["isDefault"]) == 0) ? "checked='checked'" : "") + " value=\"0\" />否</p>");
                    builder.AppendLine("<input id=\"hdUserAddress\" type=\"hidden\" value=\"" + base.Request.QueryString["id"] + "\" />");
                }
                return builder;
            }
            builder.AppendLine("<h5><strong>添加收货地址</strong></h5>");
            builder.AppendLine("<p><label for=\"txtConsignee\">收货人姓名：</label><input type=\"text\" id=\"txtConsignee\" maxlength=\"20\" /><span id=\"spConsignee\">*</span></p>");
            builder.AppendLine("<p><label for=\"selectProvinces\">省份/直辖市：</label>");
            builder.AppendLine("<select id=\"selectProvinces\">");
            builder.AppendLine("<option value=\"0\" selected=\"selected\">请选择...</option>");
            regionByParentRegionAndLanguageVersion = this.rm.GetRegionByParentRegionAndLanguageVersion(0, "zh-CN");
            foreach (DataRow row in regionByParentRegionAndLanguageVersion.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\">", row["regionName"], "</option>" }));
            }
            builder.AppendLine("</select><span id=\"spanCity\" style=\"color: #000000;\"></span><span id=\"spProvinces\">*</span></p>");
            builder.AppendLine("<p><label for=\"txtAddress\">详细地址：</label><input type=\"text\" id=\"txtAddress\" maxlength=\"200\" /><span id=\"spAddress\">*</span></p>");
            builder.AppendLine("<p><label for=\"txtZipcode\">邮政编码：</label><input type=\"text\" id=\"txtZipcode\" maxlength=\"20\" /><span id=\"spZipcode\">*</span></p>");
            builder.AppendLine("<p><label for=\"txtTelephone\"> 联系电话：</label><input type=\"text\" id=\"txtTelephone\" maxlength=\"50\" /><span id=\"spTelephone\"></span></p>");
            builder.AppendLine("<p><label for=\"txtMobile\">手机号码：</label><input type=\"text\" id=\"txtMobile\" maxlength=\"50\" /><span id=\"spMobile\">* 联系电话和手机号码至少填写一项</span></p>");
            builder.AppendLine("<p class=\"radio_input\"><label>是否默认：</label><input id=\"radioIsDefault1\" type=\"radio\" name=\"isDefault\" value=\"1\" />是&nbsp;<input id=\"radioIsDefault0\" type=\"radio\" name=\"isDefault\" checked=\"checked\" value=\"0\" />否</p>");
            builder.AppendLine("<input id=\"hdUserAddress\" type=\"hidden\" value=\"0\" />");
        }
        catch
        {
        }
        return builder;
    }

}